package com.zho.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.zho.business.AccountManager;
import com.zho.util.Helpers;
import com.zho.util.Logit;

public class SpringContextFactory {
	private static Logit log = Logit.getInstance(SpringContextFactory.class);

	// Application Context
	public static ApplicationContext context = null;

	// Manager
	private static AccountManager 	accountManager 	= null;

	private static final String SPRING_ALL_CONFIG_FILE = "service/spring-all-config.xml";
	private static final String SPRING_ACCOUNT_MANAGER_BEAN = "AccountManager";

	//AccountManager
	public static AccountManager getAccountManager() {
		try {
			if (accountManager == null) {
				accountManager = (AccountManager) getApplicationContext().getBean(SPRING_ACCOUNT_MANAGER_BEAN);
			}

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return accountManager;
	}

	public synchronized static ApplicationContext getApplicationContext() {
		try {
			if (context == null) {
				context = new FileSystemXmlApplicationContext(
						Helpers.getFullPath(SPRING_ALL_CONFIG_FILE));
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return context;
	}
}